package com.itheima.controller;

import com.itheima.domain.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Calendar;


@Controller
public class LoginController {
    @Autowired
    UserService userService;

    /** 判断是否是有效用户，如果是有效用户就进入主页，不是则返回登录
     *
      */
    @RequestMapping("/toMain")
    public String toMain(String username,String password, Model model, HttpSession session){
        //System.out.println(username);
        User yuser = null;
        if (StringUtils.hasText(username) && StringUtils.hasText(password)) {
             yuser = userService.loginUser(username, password);
        }

        if (yuser != null){
            session.setAttribute("loginUser",yuser);
            model.addAttribute("loginUser",yuser);
            return "redirect:/user/tofindAll";
        }else{
            model.addAttribute("msg","不能登录");
            //
            return "login";
        }
    }


}
